package com.xhwl.logistics.service.impl.redis;

import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.service.cent.QueryDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.hash.Jackson2HashMapper;
import org.springframework.scheduling.annotation.Async;

import javax.annotation.Resource;
import java.util.Objects;

//@Service
public class VehicleIntegratedConfigRedisServiceImpl {

    private final String vehicleIntegratedConfigPrefix = "cent-logistics:integrated-config:";

    private final String projectPrefix = "project-id:";

    private final String parkIdPrefix = "park-id:";

    @Autowired
    private QueryDeviceService queryDeviceService;

    @Resource(name = "redisTemplateForObject")
    private HashOperations<String, String, Object> hashOperations;

    @Autowired
    private Jackson2HashMapper jackson2HashMapper;

    public VehicleIntegratedConfigBO queryIntegratedConfigByProjectId(Integer projectId) {
        return (VehicleIntegratedConfigBO)jackson2HashMapper.fromHash(hashOperations.entries(vehicleIntegratedConfigPrefix + projectPrefix + projectId));
    }

    public VehicleIntegratedConfigBO queryIntegratedConfigByParkId(String parkId) {
        return (VehicleIntegratedConfigBO)jackson2HashMapper.fromHash(hashOperations.entries(vehicleIntegratedConfigPrefix + parkIdPrefix + parkId));
    }
    public Integer queryProjectIdByParkId(String parkId) {
        return queryIntegratedConfigByParkId(parkId).getVehicleSystemInfoBO().getProjectId();
    }

    @Async("threadPoolTaskExecutorForSchedule")
    public void insertOrUpdateVehicleIntegratedConfigForProjectId(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {
        hashOperations.putAll(vehicleIntegratedConfigPrefix+projectPrefix+vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId(),
                jackson2HashMapper.toHash(vehicleIntegratedConfigBO));
    }

    @Async("threadPoolTaskExecutorForSchedule")
    public void insertOrUpdateVehicleIntegratedConfigForParkId(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {
        String parkId = vehicleIntegratedConfigBO.getVehicleParkingInfoBO().getParkId();
        if (Objects.nonNull(parkId)) {
            hashOperations.putAll(vehicleIntegratedConfigPrefix+parkIdPrefix+parkId,
                    jackson2HashMapper.toHash(vehicleIntegratedConfigBO));
        }
    }

}
